package util;

import org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class FileUtil {
    public static String transform(String html) {
        String pdfFile = String.format("%s-%s.pdf", System.currentTimeMillis(), UUID.randomUUID());

        ITextRenderer renderer = new ITextRenderer();
        try {
            renderer.setDocument(html.getBytes(StandardCharsets.UTF_8));
            renderer.layout();
            try (OutputStream os = new FileOutputStream(pdfFile)) {
                renderer.createPDF(os);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return pdfFile;
    }
}